package com.lswms.modules.lswms.service.impl.task;

import java.math.BigDecimal;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.lswms.modules.lswms.entity.receipt.ReceiptEntity;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.lswms.common.page.PageData;
import com.lswms.common.service.impl.CrudServiceImpl;
import com.lswms.modules.lswms.MEGConstant;
import com.lswms.modules.lswms.Constant.TaskConstant;
import com.lswms.modules.lswms.Constant.TradeConstant;
import com.lswms.modules.lswms.dao.base.LocationDao;
import com.lswms.modules.lswms.dao.base.SkucategoryDao;
import com.lswms.modules.lswms.dao.orders.OrderdetaillistDao;
import com.lswms.modules.lswms.dao.recipient.RecipientsDao;
import com.lswms.modules.lswms.dao.stock.InventoryDao;
import com.lswms.modules.lswms.dao.stock.LotDao;
import com.lswms.modules.lswms.dao.stock.TradeDao;
import com.lswms.modules.lswms.dao.task.TaskDao;
import com.lswms.modules.lswms.dto.task.TaskDTO;
import com.lswms.modules.lswms.entity.base.LocationEntity;
import com.lswms.modules.lswms.entity.base.SkucategoryEntity;
import com.lswms.modules.lswms.entity.orders.OrderdetaillistEntity;
import com.lswms.modules.lswms.entity.recipient.RecipientsEntity;
import com.lswms.modules.lswms.entity.stock.InventoryEntity;
import com.lswms.modules.lswms.entity.stock.TradeEntity;
import com.lswms.modules.lswms.entity.task.TaskEntity;
import com.lswms.modules.lswms.service.base.SequenceService;
import com.lswms.modules.lswms.service.task.TaskService;

/**
 * 任务
 *
 * @author DX sunlightcs@gmail.com
 * @since 1.0.0 2021-06-22
 */
@Service
public class TaskServiceImpl extends CrudServiceImpl<TaskDao, TaskEntity, TaskDTO> implements TaskService {

	@Autowired
	InventoryDao inventoryDao;

	@Autowired
	OrderdetaillistDao orderdetaillistDao;

	@Autowired
	TradeDao tradeDao;

	@Autowired
	SequenceService sequenceService;

	@Autowired
	LocationDao locationDao;

	@Autowired
	LotDao lotDao;

	@Autowired
	TaskDao taskDao;

	@Autowired
	RecipientsDao recipientsDao;

	@Autowired
	SkucategoryDao skucategoryDao;

	@Override
	public QueryWrapper<TaskEntity> getWrapper(Map<String, Object> params) {
		String id = (String) params.get("id");
		String tasktype = (String) params.get("tasktype");
		String status = (String) params.get("status");

		String sku = (String) params.get("sku");

		String descr = (String) params.get("descr");
		String fromtray = (String) params.get("fromtray");
		String totray = (String) params.get("totray");

		QueryWrapper<TaskEntity> wrapper = new QueryWrapper<>();
		wrapper.eq(StringUtils.isNotBlank(id), "lswms_task.id", id);
		wrapper.eq(StringUtils.isNotBlank(tasktype), "lswms_task.tasktype", tasktype);
		wrapper.eq(StringUtils.isNotBlank(status), "lswms_task.status", status);
		// wrapper.eq(StringUtils.isNotBlank(sku), "lswms_task.sku", sku);

		wrapper.and(StringUtils.isNotBlank(sku),
				wrapper1 -> wrapper1.eq(StringUtils.isNotBlank(sku), "lswms_task.sku", sku).or()
						.eq(StringUtils.isNotBlank(sku), "lswms_sku.factorycdoe", sku));
		wrapper.like(StringUtils.isNotBlank(descr), "lswms_sku.descr", descr);
		wrapper.like(StringUtils.isNotBlank(fromtray), "lswms_task.fromtray", fromtray);
		wrapper.like(StringUtils.isNotBlank(totray), "lswms_task.totray", totray);

		wrapper.orderByDesc("taskkey");

		return wrapper;
	}

	@Override
	public PageData<TaskEntity> pageBySql(Map<String, Object> params) {
		IPage<TaskEntity> page = baseDao.selectPageBySql(getPage(params, null, false), getWrapper(params));
		return getPageData(page, TaskEntity.class);
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String done(List<TaskEntity> list) {

		for (TaskEntity taskEntity : list) {
			String msg = done(taskEntity);
		}
		return null;
	}

	private String done(TaskEntity taskEntity) {
		// 查询任务信息
		QueryWrapper<TaskEntity> taskQuery = new QueryWrapper<TaskEntity>();
		taskQuery.eq("taskkey", taskEntity.getTaskkey());

		TaskEntity taskEntityCurrent = baseDao.selectOne(taskQuery);
		if (taskEntityCurrent.getStatus() == TaskConstant.TASK_9) {
			return null;
		}

		// 设置库存变动
		// fromloc 的库位减库存，
		QueryWrapper<InventoryEntity> inventoryFormlocQuery = new QueryWrapper<InventoryEntity>();
		inventoryFormlocQuery.eq("sku", taskEntityCurrent.getSku());
		inventoryFormlocQuery.eq("location", taskEntityCurrent.getFromlocation());
		inventoryFormlocQuery.eq("lot", taskEntityCurrent.getFromlot());
		inventoryFormlocQuery.eq("tray", taskEntityCurrent.getFromtray());
		inventoryFormlocQuery.eq("box", taskEntityCurrent.getFrombox());
		InventoryEntity inventoryEntityFromloc = inventoryDao.selectOne(inventoryFormlocQuery);
		if (inventoryEntityFromloc.getQty().compareTo(taskEntityCurrent.getQty()) < 0) {
			// 回滚事务
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			return MEGConstant.INVENTORY_PICKQTY_NOT_MATCH_TASK_ACTQTY;
		}
		UpdateWrapper<InventoryEntity> inventoryFormlocUpdate = new UpdateWrapper<InventoryEntity>();
		inventoryFormlocUpdate.eq("sku", taskEntityCurrent.getSku());
		inventoryFormlocUpdate.eq("location", taskEntityCurrent.getFromlocation());
		inventoryFormlocUpdate.eq("lot", taskEntityCurrent.getFromlot());
		inventoryFormlocUpdate.eq("tray", taskEntityCurrent.getFromtray());
		inventoryFormlocUpdate.eq("box", taskEntityCurrent.getFrombox());
		inventoryFormlocUpdate.set("qty", inventoryEntityFromloc.getQty().subtract(taskEntityCurrent.getQty()));
		if (taskEntityCurrent.getTasktype().equals(TaskConstant.TASK_PICK)
				|| taskEntityCurrent.getTasktype().equals(TaskConstant.TASK_PY)) {
			if (inventoryEntityFromloc.getPickqty().compareTo(taskEntityCurrent.getQty()) < 0) {
				// 回滚事务
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
				return MEGConstant.INVENTORY_PICKQTY_NOT_MATCH_TASK_ACTQTY;
			}
			inventoryFormlocUpdate.set("pickqty",
					inventoryEntityFromloc.getPickqty().subtract(taskEntityCurrent.getQty()));
		}
		inventoryDao.update(null, inventoryFormlocUpdate);

		// toloc的库位增加库存
		QueryWrapper<InventoryEntity> inventoryTolocQuery = new QueryWrapper<InventoryEntity>();
		inventoryTolocQuery.eq("sku", taskEntityCurrent.getSku());
		inventoryTolocQuery.eq("location", taskEntityCurrent.getTolocation());
		inventoryTolocQuery.eq("lot", taskEntityCurrent.getTolot());
		inventoryTolocQuery.eq("tray", taskEntityCurrent.getTotray());
		inventoryTolocQuery.eq("box", taskEntityCurrent.getTobox());
		InventoryEntity inventoryEntityToloc = inventoryDao.selectOne(inventoryTolocQuery);
		if (inventoryEntityToloc == null) {
			// 库存记录不存在，增加库存记录
			InventoryEntity inventoryTolocInsert = new InventoryEntity();
			inventoryTolocInsert.setSku(taskEntityCurrent.getSku());
			inventoryTolocInsert.setLocation(taskEntityCurrent.getTolocation());
			inventoryTolocInsert.setLot(taskEntityCurrent.getTolot());
			inventoryTolocInsert.setTray(taskEntityCurrent.getTotray());
			inventoryTolocInsert.setBox(taskEntityCurrent.getTobox());
			inventoryTolocInsert.setQty(taskEntityCurrent.getQty());
			if (taskEntityCurrent.getTasktype().equals(TaskConstant.TASK_PICK)
					|| taskEntityCurrent.getTasktype().equals(TaskConstant.TASK_PY)) {
				inventoryTolocInsert.setPickqty(taskEntityCurrent.getQty());
			}
			inventoryTolocInsert.setPutqty(new BigDecimal(0));
			inventoryTolocInsert.setStatus(0);
			inventoryDao.insert(inventoryTolocInsert);
		} else {
			UpdateWrapper<InventoryEntity> inventoryTolocUpdate = new UpdateWrapper<InventoryEntity>();
			inventoryTolocUpdate.eq("sku", taskEntityCurrent.getSku());
			inventoryTolocUpdate.eq("location", taskEntityCurrent.getTolocation());
			inventoryTolocUpdate.eq("lot", taskEntityCurrent.getTolot());
			inventoryTolocUpdate.eq("tray", taskEntityCurrent.getTotray());
			inventoryTolocUpdate.eq("box", taskEntityCurrent.getTobox());
			inventoryTolocUpdate.set("qty", inventoryEntityToloc.getQty().add(taskEntityCurrent.getQty()));
			if (taskEntityCurrent.getTasktype().equals(TaskConstant.TASK_PICK)
					|| taskEntityCurrent.getTasktype().equals(TaskConstant.TASK_PY)) {
				inventoryTolocUpdate.set("pickqty", inventoryEntityToloc.getPickqty().add(taskEntityCurrent.getQty()));
			} else if (taskEntityCurrent.getTasktype().equals(TaskConstant.TASK_PA)) {
				if (inventoryEntityToloc.getPutqty().compareTo(taskEntityCurrent.getQty()) < 0) {
					// 回滚事务
					TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
					return MEGConstant.INVENTORY_PICKQTY_NOT_MATCH_TASK_ACTQTY;
				}
				inventoryTolocUpdate.set("putqty",
						inventoryEntityToloc.getPutqty().subtract(taskEntityCurrent.getQty()));
			}
			inventoryDao.update(null, inventoryTolocUpdate);
		}

		if (taskEntityCurrent.getTasktype().equals(TaskConstant.TASK_PICK)) {
			// 如果是出库任务，变更拣货明细的当前库位信息
			UpdateWrapper<OrderdetaillistEntity> orderdetaillistUpdate = new UpdateWrapper<OrderdetaillistEntity>();
			orderdetaillistUpdate.eq("orderkey", taskEntityCurrent.getSourcekey());
			orderdetaillistUpdate.eq("line", taskEntityCurrent.getSourceline());
			orderdetaillistUpdate.eq("linelist", taskEntityCurrent.getSourcelinelist());
			orderdetaillistUpdate.set("loc", taskEntityCurrent.getTolocation());
			orderdetaillistUpdate.set("tray", taskEntityCurrent.getTotray());
			orderdetaillistUpdate.set("box", taskEntityCurrent.getTobox());
			orderdetaillistUpdate.set("actqty", taskEntityCurrent.getQty());
			orderdetaillistDao.update(null, orderdetaillistUpdate);
		}
		if (taskEntityCurrent.getTasktype().equals(TaskConstant.TASK_PY)) {
			// 如果是出库任务，变更拣货明细的当前库位信息
			UpdateWrapper<RecipientsEntity> RecipientsEntityUpdate = new UpdateWrapper<RecipientsEntity>();
			RecipientsEntityUpdate.eq("recipientkey", taskEntityCurrent.getSourcekey());
			RecipientsEntityUpdate.set("loc", taskEntityCurrent.getTolocation());
			RecipientsEntityUpdate.set("tray", taskEntityCurrent.getTotray());
			RecipientsEntityUpdate.set("box", taskEntityCurrent.getTobox());
			RecipientsEntityUpdate.set("qty", taskEntityCurrent.getQty());
			recipientsDao.update(null, RecipientsEntityUpdate);
		}

		// 增加交易信息
		String tradekey = sequenceService.getSequence("trade");
		TradeEntity tradeEntity = new TradeEntity();

		tradeEntity.setTradekey(tradekey);
		tradeEntity.setTradetype(TradeConstant.TRADETYPE_MV);
		tradeEntity.setSku(taskEntityCurrent.getSku());
		tradeEntity.setQty(taskEntityCurrent.getQty());
		tradeEntity.setSourcetype(TradeConstant.SOURCETYPE_MOVE);
		tradeEntity.setSourcekey(taskEntityCurrent.getTaskkey());
		tradeEntity.setSourceline(null);
		tradeEntity.setSourcelinelist(null);
		tradeEntity.setFromlocation(taskEntityCurrent.getFromlocation());
		tradeEntity.setTolocation(taskEntityCurrent.getTolocation());
		tradeEntity.setFromtray(taskEntityCurrent.getFromtray());
		tradeEntity.setTotray(taskEntityCurrent.getTotray());
		tradeEntity.setFrombox(taskEntityCurrent.getFrombox());
		tradeEntity.setTobox(taskEntityCurrent.getTobox());
		tradeEntity.setFromlot(taskEntityCurrent.getFromlot());
		tradeEntity.setTolot(taskEntityCurrent.getTolot());
		tradeEntity.setFromlot1(null);
		tradeEntity.setFromlot2(null);
		tradeEntity.setFromlot3("");
		tradeEntity.setFromlot4("");
		tradeEntity.setFromlot5("");
		tradeEntity.setFromlot6("");
		tradeEntity.setFromlot7("");
		tradeEntity.setFromlot8("");
		tradeEntity.setFromlot9("");
		tradeEntity.setFromlot10("");
		tradeEntity.setFromlot11("");
		tradeEntity.setFromlot12("");
		tradeEntity.setFromlot13("");
		tradeEntity.setFromlot14("");
		tradeEntity.setFromlot15("");
		tradeEntity.setTolot1(null);
		tradeEntity.setTolot2(null);
		tradeEntity.setTolot3("");
		tradeEntity.setTolot4("");
		tradeEntity.setTolot5("");
		tradeEntity.setTolot6("");
		tradeEntity.setTolot7("");
		tradeEntity.setTolot8("");
		tradeEntity.setTolot9("");
		tradeEntity.setTolot10("");
		tradeEntity.setTolot11("");
		tradeEntity.setTolot12("");
		tradeEntity.setTolot13("");
		tradeEntity.setTolot14("");
		tradeEntity.setTolot15("");
		tradeDao.insert(tradeEntity);

		// 更新任务的状态
		UpdateWrapper<TaskEntity> taskUpdate = new UpdateWrapper<TaskEntity>();
		taskUpdate.eq("taskkey", taskEntityCurrent.getTaskkey());
		taskUpdate.set("status", TaskConstant.TASK_9);
		int update = baseDao.update(null, taskUpdate);

		return null;
	}

	@Override
	public String valideMove(TaskEntity taskEntity) {
		// 校验选择的托盘是否有其他任务
		QueryWrapper<TaskEntity> taskFromQuery = new QueryWrapper<TaskEntity>();
		taskFromQuery.ne("status", 9);
		taskFromQuery.and(wapper -> wapper
				.and(wapper2 -> wapper2.eq("fromlocation", taskEntity.getFromlocation()).eq("fromtray",
						taskEntity.getFromtray()))
				.or(wapper1 -> wapper1.eq("tolocation", taskEntity.getFromlocation()).eq("totray",
						taskEntity.getFromtray())));
		Integer countFrom = baseDao.selectCount(taskFromQuery);
		if (countFrom > 0) {
			return MEGConstant.TRAY_HAS_OTHER_TASK;
		}

		// 查询选择的托盘是否有分配任务或上架任务
		QueryWrapper<InventoryEntity> inventoryQuery = new QueryWrapper<InventoryEntity>();
		inventoryQuery.eq("location", taskEntity.getFromlocation());
		inventoryQuery.eq("tray", taskEntity.getFromtray());
		inventoryQuery.and(wrapp -> wrapp.gt("pickqty", 0).or(wrapp1 -> wrapp1.gt("putqty", 0)));
		Integer countInventory = inventoryDao.selectCount(inventoryQuery);
		if (countInventory > 0) {
			return MEGConstant.TRAY_HAS_OTHER_TASK;
		}

		// 查询目的库位信息
		QueryWrapper<LocationEntity> locationToQuery = new QueryWrapper<LocationEntity>();
		locationToQuery.eq("location", taskEntity.getTolocation());
		LocationEntity locationEntityTo = locationDao.selectOne(locationToQuery);
		if (locationEntityTo == null) {
			return String.format(MEGConstant.LOC_NOT_EXISTS, taskEntity.getTolocation());
		}

		String ismixedsku = locationEntityTo.getIsmixedsku();
		String ismixedtray = locationEntityTo.getIsmixedtray();
		String ismixedlot1 = locationEntityTo.getIsmixedlot1();
		String ismixedlot2 = locationEntityTo.getIsmixedlot2();
		String ismixedlot3 = locationEntityTo.getIsmixedlot3();
		String ismixedlot4 = locationEntityTo.getIsmixedlot4();
		String ismixedlot5 = locationEntityTo.getIsmixedlot5();
		String ismixedlot6 = locationEntityTo.getIsmixedlot6();
		String ismixedlot7 = locationEntityTo.getIsmixedlot7();
		String ismixedlot8 = locationEntityTo.getIsmixedlot8();
		String ismixedlot9 = locationEntityTo.getIsmixedlot9();
		String ismixedlot10 = locationEntityTo.getIsmixedlot10();
		String ismixedlot11 = locationEntityTo.getIsmixedlot11();
		String ismixedlot12 = locationEntityTo.getIsmixedlot12();
		String ismixedlot13 = locationEntityTo.getIsmixedlot13();
		String ismixedlot14 = locationEntityTo.getIsmixedlot14();
		String ismixedlot15 = locationEntityTo.getIsmixedlot15();

		// 查询选择托盘的库存信息
		QueryWrapper<InventoryEntity> inventoryFromQuery = new QueryWrapper<InventoryEntity>();
		inventoryFromQuery.eq("location", taskEntity.getFromlocation());
		inventoryFromQuery.eq("tray", taskEntity.getFromtray());
		List<InventoryEntity> inventoryFromList = inventoryDao.selectInventoryAndLot(inventoryQuery);

		// 检查目的库位是否存空闲
		QueryWrapper<InventoryEntity> inventoryToQuery = new QueryWrapper<InventoryEntity>();
		inventoryToQuery.eq("location", taskEntity.getTolocation());
		inventoryToQuery.gt("qty", 0);
		List<InventoryEntity> inventoryToList = inventoryDao.selectInventoryAndLot(inventoryToQuery);
		if (inventoryToList.size() > 0 && ismixedtray.equals("1")) {
			return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_TRAY;
		}
		for (InventoryEntity inventoryToEntity : inventoryToList) {
			String skuTo = inventoryToEntity.getSku();

			for (InventoryEntity inventoryFromEntity : inventoryFromList) {

				String skuFrom = inventoryFromEntity.getSku();
				if (ismixedsku.equals("1")) {
					if (!skuTo.equals(skuFrom)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_SKU;
					}
				}

				if (ismixedlot1.equals("1")) {
					Date lot1To = inventoryToEntity.getLot1();
					Date lot1From = inventoryFromEntity.getLot1();
					if ((lot1To == null && lot1From != null) || (lot1To != null && lot1From == null)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT1;
					}
					if ((lot1To != null && lot1From != null) && !lot1To.equals(lot1From)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT1;
					}
				}
				if (ismixedlot2.equals("1")) {
					Date lot2To = inventoryToEntity.getLot2();
					Date lot2From = inventoryFromEntity.getLot2();
					if ((lot2To == null && lot2From != null) || (lot2To != null && lot2From == null)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT2;
					}
					if ((lot2To != null && lot2From != null) && !lot2To.equals(lot2From)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT2;
					}
				}

				if (ismixedlot3.equals("1")) {
					String lot3To = inventoryToEntity.getLot3();
					String lot3From = inventoryFromEntity.getLot3();
					if ((lot3To == null && lot3From != null) || (lot3To != null && lot3From == null)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT3;
					}
					if ((lot3To != null && lot3From != null) && !lot3To.equals(lot3From)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT3;
					}
				}

				if (ismixedlot4.equals("1")) {
					String lot4To = inventoryToEntity.getLot4();
					String lot4From = inventoryFromEntity.getLot4();
					if ((lot4To == null && lot4From != null) || (lot4To != null && lot4From == null)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT4;
					}
					if ((lot4To != null && lot4From != null) && !lot4To.equals(lot4From)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT4;
					}
				}
				if (ismixedlot5.equals("1")) {
					String lot5To = inventoryToEntity.getLot5();
					String lot5From = inventoryFromEntity.getLot5();
					if ((lot5To == null && lot5From != null) || (lot5To != null && lot5From == null)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT5;
					}
					if ((lot5To != null && lot5From != null) && !lot5To.equals(lot5From)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT5;
					}
				}
				if (ismixedlot6.equals("1")) {
					String lot6To = inventoryToEntity.getLot6();
					String lot6From = inventoryFromEntity.getLot6();
					if ((lot6To == null && lot6From != null) || (lot6To != null && lot6From == null)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT6;
					}
					if ((lot6To != null && lot6From != null) && !lot6To.equals(lot6From)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT6;
					}
				}

				if (ismixedlot7.equals("1")) {
					String lot7To = inventoryToEntity.getLot7();
					String lot7From = inventoryFromEntity.getLot7();
					if ((lot7To == null && lot7From != null) || (lot7To != null && lot7From == null)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT7;
					}
					if ((lot7To != null && lot7From != null) && !lot7To.equals(lot7From)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT7;
					}
				}

				if (ismixedlot8.equals("1")) {
					String lot8To = inventoryToEntity.getLot8();
					String lot8From = inventoryFromEntity.getLot8();
					if ((lot8To == null && lot8From != null) || (lot8To != null && lot8From == null)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT8;
					}
					if ((lot8To != null && lot8From != null) && !lot8To.equals(lot8From)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT8;
					}
				}

				if (ismixedlot9.equals("1")) {
					String lot9To = inventoryToEntity.getLot9();
					String lot9From = inventoryFromEntity.getLot9();
					if ((lot9To == null && lot9From != null) || (lot9To != null && lot9From == null)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT9;
					}
					if ((lot9To != null && lot9From != null) && !lot9To.equals(lot9From)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT9;
					}
				}
				if (ismixedlot10.equals("1")) {
					String lot10To = inventoryToEntity.getLot10();
					String lot10From = inventoryFromEntity.getLot10();
					if ((lot10To == null && lot10From != null) || (lot10To != null && lot10From == null)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT10;
					}
					if ((lot10To != null && lot10From != null) && !lot10To.equals(lot10From)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT10;
					}
				}
				if (ismixedlot11.equals("1")) {
					String lot11To = inventoryToEntity.getLot11();
					String lot11From = inventoryFromEntity.getLot11();
					if ((lot11To == null && lot11From != null) || (lot11To != null && lot11From == null)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT11;
					}
					if ((lot11To != null && lot11From != null) && !lot11To.equals(lot11From)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT11;
					}
				}
				if (ismixedlot12.equals("1")) {
					String lot12To = inventoryToEntity.getLot12();
					String lot12From = inventoryFromEntity.getLot12();
					if ((lot12To == null && lot12From != null) || (lot12To != null && lot12From == null)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT12;
					}
					if ((lot12To != null && lot12From != null) && !lot12To.equals(lot12From)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT12;
					}
				}
				if (ismixedlot13.equals("1")) {
					String lot13To = inventoryToEntity.getLot13();
					String lot13From = inventoryFromEntity.getLot13();
					if ((lot13To == null && lot13From != null) || (lot13To != null && lot13From == null)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT13;
					}
					if ((lot13To != null && lot13From != null) && !lot13To.equals(lot13From)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT13;
					}
				}
				if (ismixedlot14.equals("1")) {
					String lot14To = inventoryToEntity.getLot14();
					String lot14From = inventoryFromEntity.getLot14();
					if ((lot14To == null && lot14From != null) || (lot14To != null && lot14From == null)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT14;
					}
					if ((lot14To != null && lot14From != null) && !lot14To.equals(lot14From)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT14;
					}
				}
				if (ismixedlot15.equals("1")) {
					String lot15To = inventoryToEntity.getLot15();
					String lot15From = inventoryFromEntity.getLot15();
					if ((lot15To == null && lot15From != null) || (lot15To != null && lot15From == null)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT15;
					}
					if ((lot15To != null && lot15From != null) && !lot15To.equals(lot15From)) {
						return MEGConstant.TO_LOCATION_ISNOTMIXED_BY_LOT15;
					}
				}
			}
		}

		// 检查目的库位是否存在其他任务
		QueryWrapper<TaskEntity> taskToQuery = new QueryWrapper<TaskEntity>();
		taskToQuery.ne("status", 9);
		taskToQuery.eq("tolocation", taskEntity.getTolocation());
		List<TaskEntity> taskToList = baseDao.selectTaskAndLot(taskToQuery);
		if (taskToList.size() > 0 && ismixedtray.equals("1")) {
			return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_TRAY;
		}

		for (TaskEntity taskToEntity : taskToList) {
			String skuTo = taskToEntity.getSku();

			for (InventoryEntity inventoryFromEntity : inventoryFromList) {

				String skuFrom = inventoryFromEntity.getSku();
				if (ismixedsku.equals("1")) {
					if (!skuTo.equals(skuFrom)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_SKU;
					}
				}

				if (ismixedlot1.equals("1")) {
					Date lot1To = taskToEntity.getLot1();
					Date lot1From = inventoryFromEntity.getLot1();
					if ((lot1To == null && lot1From != null) || (lot1To != null && lot1From == null)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT1;
					}
					if ((lot1To != null && lot1From != null) && !lot1To.equals(lot1From)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT1;
					}
				}
				if (ismixedlot2.equals("1")) {
					Date lot2To = taskToEntity.getLot2();
					Date lot2From = inventoryFromEntity.getLot2();
					if ((lot2To == null && lot2From != null) || (lot2To != null && lot2From == null)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT2;
					}
					if ((lot2To != null && lot2From != null) && !lot2To.equals(lot2From)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT2;
					}
				}

				if (ismixedlot3.equals("1")) {
					String lot3To = taskToEntity.getLot3();
					String lot3From = inventoryFromEntity.getLot3();
					if ((lot3To == null && lot3From != null) || (lot3To != null && lot3From == null)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT3;
					}
					if ((lot3To != null && lot3From != null) && !lot3To.equals(lot3From)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT3;
					}
				}

				if (ismixedlot4.equals("1")) {
					String lot4To = taskToEntity.getLot4();
					String lot4From = inventoryFromEntity.getLot4();
					if ((lot4To == null && lot4From != null) || (lot4To != null && lot4From == null)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT4;
					}
					if ((lot4To != null && lot4From != null) && !lot4To.equals(lot4From)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT4;
					}
				}
				if (ismixedlot5.equals("1")) {
					String lot5To = taskToEntity.getLot5();
					String lot5From = inventoryFromEntity.getLot5();
					if ((lot5To == null && lot5From != null) || (lot5To != null && lot5From == null)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT5;
					}
					if ((lot5To != null && lot5From != null) && !lot5To.equals(lot5From)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT5;
					}
				}
				if (ismixedlot6.equals("1")) {
					String lot6To = taskToEntity.getLot6();
					String lot6From = inventoryFromEntity.getLot6();
					if ((lot6To == null && lot6From != null) || (lot6To != null && lot6From == null)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT6;
					}
					if ((lot6To != null && lot6From != null) && !lot6To.equals(lot6From)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT6;
					}
				}

				if (ismixedlot7.equals("1")) {
					String lot7To = taskToEntity.getLot7();
					String lot7From = inventoryFromEntity.getLot7();
					if ((lot7To == null && lot7From != null) || (lot7To != null && lot7From == null)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT7;
					}
					if ((lot7To != null && lot7From != null) && !lot7To.equals(lot7From)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT7;
					}
				}

				if (ismixedlot8.equals("1")) {
					String lot8To = taskToEntity.getLot8();
					String lot8From = inventoryFromEntity.getLot8();
					if ((lot8To == null && lot8From != null) || (lot8To != null && lot8From == null)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT8;
					}
					if ((lot8To != null && lot8From != null) && !lot8To.equals(lot8From)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT8;
					}
				}

				if (ismixedlot9.equals("1")) {
					String lot9To = taskToEntity.getLot9();
					String lot9From = inventoryFromEntity.getLot9();
					if ((lot9To == null && lot9From != null) || (lot9To != null && lot9From == null)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT9;
					}
					if ((lot9To != null && lot9From != null) && !lot9To.equals(lot9From)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT9;
					}
				}
				if (ismixedlot10.equals("1")) {
					String lot10To = taskToEntity.getLot10();
					String lot10From = inventoryFromEntity.getLot10();
					if ((lot10To == null && lot10From != null) || (lot10To != null && lot10From == null)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT10;
					}
					if ((lot10To != null && lot10From != null) && !lot10To.equals(lot10From)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT10;
					}
				}

				if (ismixedlot11.equals("1")) {
					String lot11To = taskToEntity.getLot11();
					String lot11From = inventoryFromEntity.getLot11();
					if ((lot11To == null && lot11From != null) || (lot11To != null && lot11From == null)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT11;
					}
					if ((lot11To != null && lot11From != null) && !lot11To.equals(lot11From)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT11;
					}
				}
				if (ismixedlot12.equals("1")) {
					String lot12To = taskToEntity.getLot12();
					String lot12From = inventoryFromEntity.getLot12();
					if ((lot12To == null && lot12From != null) || (lot12To != null && lot12From == null)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT12;
					}
					if ((lot12To != null && lot12From != null) && !lot12To.equals(lot12From)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT12;
					}
				}
				if (ismixedlot13.equals("1")) {
					String lot13To = taskToEntity.getLot13();
					String lot13From = inventoryFromEntity.getLot13();
					if ((lot13To == null && lot13From != null) || (lot13To != null && lot13From == null)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT13;
					}
					if ((lot13To != null && lot13From != null) && !lot13To.equals(lot13From)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT13;
					}
				}
				if (ismixedlot14.equals("1")) {
					String lot14To = taskToEntity.getLot14();
					String lot14From = inventoryFromEntity.getLot14();
					if ((lot14To == null && lot14From != null) || (lot14To != null && lot14From == null)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT14;
					}
					if ((lot14To != null && lot14From != null) && !lot14To.equals(lot14From)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT14;
					}
				}
				if (ismixedlot15.equals("1")) {
					String lot15To = taskToEntity.getLot15();
					String lot15From = inventoryFromEntity.getLot15();
					if ((lot15To == null && lot15From != null) || (lot15To != null && lot15From == null)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT15;
					}
					if ((lot15To != null && lot15From != null) && !lot15To.equals(lot15From)) {
						return MEGConstant.TO_LOCATION_HAS_TASK_ISNOTMIXED_BY_LOT15;
					}
				}
			}
		}
		return null;
	}

	@Override
	@Transactional(rollbackFor = Exception.class)
	public String move(TaskEntity taskEntity, String remoteAddr) {

		// 查询库位类别，给前端提示
		String fromloc = taskEntity.getFromlocation();
		String toloc = taskEntity.getTolocation();

		String fromLocIsCreate = "";
		String toLocIsCreate = "";

		QueryWrapper<LocationEntity> locationQuery = new QueryWrapper<LocationEntity>();
		locationQuery.in("location", fromloc, toloc);
		List<LocationEntity> list1 = locationDao.selectList(locationQuery);
		for (LocationEntity locationEntity : list1) {
			if (locationEntity.getLocation().equals(fromloc)) {
				fromLocIsCreate = locationEntity.getIscreate();
			}
			if (locationEntity.getLocation().equals(toloc)) {
				toLocIsCreate = locationEntity.getIscreate();
			}
		}

		// 如果是立体库位,校验堆垛机是否可用
		QueryWrapper<LocationEntity> queryLocation = new QueryWrapper<LocationEntity>();
		queryLocation.eq("location", fromloc);
		queryLocation
				.exists(" select  1 from WCS_DDJ where lswms_location.lane = WCS_DDJ.DVC_NO and WCS_DDJ.status <>'1' ");

		Integer count1 = locationDao.selectCount(queryLocation);
		if (count1 == 1) {
			return "选择的库位" + fromloc + "的堆垛机已停用，无法移库";
		}
		// 如果是立体库位,校验堆垛机是否可用
		QueryWrapper<LocationEntity> queryLocation1 = new QueryWrapper<LocationEntity>();
		queryLocation1.eq("location", toloc);
		queryLocation1
				.exists(" select  1 from WCS_DDJ where lswms_location.lane = WCS_DDJ.DVC_NO and WCS_DDJ.status <>'1' ");

		Integer count2 = locationDao.selectCount(queryLocation);
		if (count2 == 1) {
			return "选择的库位" + toloc + "的堆垛机已停用，无法移库";
		}

		// 查询fromloc和tray的库存记录，生成移库任务
		QueryWrapper<InventoryEntity> inventoryQuery = new QueryWrapper<InventoryEntity>();
		inventoryQuery.eq("location", taskEntity.getFromlocation());
		inventoryQuery.eq("tray", taskEntity.getFromtray());
		inventoryQuery.gt("qty", 0);
		List<InventoryEntity> list = inventoryDao.selectList(inventoryQuery);
		for (InventoryEntity inventoryEntity : list) {
			if (StringUtils.isNotBlank(fromLocIsCreate) && fromLocIsCreate.equals("1")
					|| StringUtils.isNotBlank(toLocIsCreate) && toLocIsCreate.equals("1")) {

				String taskkey = sequenceService.getSequence("task");

				TaskEntity taskEntityAdd = new TaskEntity();
				taskEntityAdd.setTaskkey(taskkey);
				taskEntityAdd.setTasktype(TaskConstant.TASK_MV);
				taskEntityAdd.setPriority(0);
				taskEntityAdd.setStatus(TaskConstant.TASK_0);
				taskEntityAdd.setSku(inventoryEntity.getSku());
				taskEntityAdd.setQty(inventoryEntity.getQty());
				taskEntityAdd.setFromlocation(inventoryEntity.getLocation());
				taskEntityAdd.setTolocation(taskEntity.getTolocation());
				taskEntityAdd.setFromtray(inventoryEntity.getTray());
				taskEntityAdd.setTotray(inventoryEntity.getTray());
				taskEntityAdd.setFrombox(inventoryEntity.getBox());
				taskEntityAdd.setTobox(inventoryEntity.getBox());
				taskEntityAdd.setFromlot(inventoryEntity.getLot());
				taskEntityAdd.setTolot(inventoryEntity.getLot());
				taskEntityAdd.setIp(remoteAddr);
				baseDao.insert(taskEntityAdd);
			} else {
				// 起始库位扣减库存
				InventoryEntity updateInventoryEntityFromloc = new InventoryEntity();
				updateInventoryEntityFromloc.setQty(BigDecimal.ZERO);
				UpdateWrapper<InventoryEntity> updateInventoryFromloc = new UpdateWrapper<InventoryEntity>();
				updateInventoryFromloc.eq("lot", inventoryEntity.getLot());
				updateInventoryFromloc.eq("location", inventoryEntity.getLocation());
				updateInventoryFromloc.eq("sku", inventoryEntity.getSku());
				updateInventoryFromloc.eq("tray", inventoryEntity.getTray());
				updateInventoryFromloc.eq("box", inventoryEntity.getBox());
				inventoryDao.update(updateInventoryEntityFromloc, updateInventoryFromloc);

				// 检查目的库位是否有已有相同的库存记录
				QueryWrapper<InventoryEntity> queryInventoryToloc = new QueryWrapper<InventoryEntity>();
				queryInventoryToloc.eq("lot", inventoryEntity.getLot());
				queryInventoryToloc.eq("location", toloc);
				queryInventoryToloc.eq("sku", inventoryEntity.getSku());
				queryInventoryToloc.eq("tray", inventoryEntity.getTray());
				queryInventoryToloc.eq("box", inventoryEntity.getBox());
				InventoryEntity inventoryToloc = inventoryDao.selectOne(queryInventoryToloc);
				if (inventoryToloc != null) {
					InventoryEntity updateInventoryEntity = new InventoryEntity();
					updateInventoryEntity.setQty(inventoryToloc.getQty().add(inventoryEntity.getQty()));
					UpdateWrapper<InventoryEntity> updateInventory = new UpdateWrapper<InventoryEntity>();
					updateInventory.eq("lot", inventoryEntity.getLot());
					updateInventory.eq("location", toloc);
					updateInventory.eq("sku", inventoryEntity.getSku());
					updateInventory.eq("tray", inventoryEntity.getTray());
					updateInventory.eq("box", inventoryEntity.getBox());
					inventoryDao.update(updateInventoryEntity, updateInventory);
				} else {
					InventoryEntity addInventoryEntity = new InventoryEntity();
					addInventoryEntity.setSku(inventoryEntity.getSku());
					addInventoryEntity.setLot(inventoryEntity.getLot());
					addInventoryEntity.setLocation(toloc);
					addInventoryEntity.setTray(inventoryEntity.getTray());
					addInventoryEntity.setBox(inventoryEntity.getBox());
					addInventoryEntity.setQty(inventoryEntity.getQty());
					addInventoryEntity.setPickqty(new BigDecimal(0));
					addInventoryEntity.setPutqty(new BigDecimal(0));
					addInventoryEntity.setStatus(0);
					addInventoryEntity.setRemark(inventoryEntity.getRemark());
					inventoryDao.insert(addInventoryEntity);
				}

				String tradekey = sequenceService.getSequence("trade");
				TradeEntity tradeEntity = new TradeEntity();

				tradeEntity.setTradekey(tradekey);
				tradeEntity.setTradetype(TradeConstant.TRADETYPE_MV);
				tradeEntity.setSku(inventoryEntity.getSku());
				tradeEntity.setQty(inventoryEntity.getQty());
				tradeEntity.setSourcetype(TradeConstant.SOURCETYPE_MOVE);
				tradeEntity.setSourcekey(null);
				tradeEntity.setSourceline(null);
				tradeEntity.setSourcelinelist(null);
				tradeEntity.setFromlocation(inventoryEntity.getLocation());
				tradeEntity.setTolocation(toloc);
				tradeEntity.setFromtray(inventoryEntity.getTray());
				tradeEntity.setTotray(inventoryEntity.getTray());
				tradeEntity.setFrombox(inventoryEntity.getBox());
				tradeEntity.setTobox(inventoryEntity.getBox());
				tradeEntity.setFromlot(inventoryEntity.getLot());
				tradeEntity.setTolot(inventoryEntity.getLot());
				tradeEntity.setFromlot1(null);
				tradeEntity.setFromlot2(null);
				tradeEntity.setFromlot3("");
				tradeEntity.setFromlot4("");
				tradeEntity.setFromlot5("");
				tradeEntity.setFromlot6("");
				tradeEntity.setFromlot7("");
				tradeEntity.setFromlot8("");
				tradeEntity.setFromlot9("");
				tradeEntity.setFromlot10("");
				tradeEntity.setFromlot11("");
				tradeEntity.setFromlot12("");
				tradeEntity.setFromlot13("");
				tradeEntity.setFromlot14("");
				tradeEntity.setFromlot15("");
				tradeEntity.setTolot1(null);
				tradeEntity.setTolot2(null);
				tradeEntity.setTolot3("");
				tradeEntity.setTolot4("");
				tradeEntity.setTolot5("");
				tradeEntity.setTolot6("");
				tradeEntity.setTolot7("");
				tradeEntity.setTolot8("");
				tradeEntity.setTolot9("");
				tradeEntity.setTolot10("");
				tradeEntity.setTolot11("");
				tradeEntity.setTolot12("");
				tradeEntity.setTolot13("");
				tradeEntity.setTolot14("");
				tradeEntity.setTolot15("");
				tradeDao.insert(tradeEntity);

			}
		}

		// 查询库位类别，给前端提示
//		String fromloc = taskEntity.getFromlocation();
//		String toloc = taskEntity.getTolocation();
//
//		String fromLocCategory = "";
//		String toLocCategory = "";
//
//		QueryWrapper<LocationEntity> locationQuery = new QueryWrapper<LocationEntity>();
//		locationQuery.in("location", fromloc, toloc);
//		List<LocationEntity> list1 = locationDao.selectList(locationQuery);
//		for (LocationEntity locationEntity : list1) {
//			if (locationEntity.getLocation().equals(fromloc)) {
//				fromLocCategory = locationEntity.getLocCategory();
//			}
//			if (locationEntity.getLocation().equals(toloc)) {
//				toLocCategory = locationEntity.getLocCategory();
//			}
//		}

		if (!fromLocIsCreate.equals("1") && !toLocIsCreate.equals("1")) {
			return MEGConstant.TASK_1;
		} else if (fromLocIsCreate.equals("1") && !toLocIsCreate.equals("1")) {
			return MEGConstant.TASK_2;
		} else if (!fromLocIsCreate.equals("1") && toLocIsCreate.equals("1")) {
			return MEGConstant.TASK_3;
		}
		if (fromLocIsCreate.equals("1") && toLocIsCreate.equals("1")) {
			return MEGConstant.TASK_4;
		}

		return null;
	}

	@Override
	public List<Map<String, Object>> getStatusList() {
		int pa = 0;
		int MV = 0;
		int PICK = 0;
		int CC = 0;

		List<Map<String, Object>> statusList = taskDao.getStatusList();
//		for (Map map: statusList) {
//			
////			tasktype	allcount	undo
////			MV	2	0
//			String taskType = (String) map.get("tasktype") ;
//			String allcount =  (String) map.get("allcount") ;
//			String undo =  (String) map.get("undo") ;
//			
//			if (item.getTasktype().equals("PA")) {
//				pa++;
//			}
//			if (item.getTasktype().equals("MV")) {
//				MV++;
//			}
//			if (item.getTasktype().equals("PICK")) {
//				PICK++;
//			}
//			if (item.getTasktype().equals("CC")) {
//				CC++;
//			}
//		}
//		HashMap<String, Integer> maps = new HashMap<>();
//		maps.put("PA", pa);
//		maps.put("MV", MV);
//		maps.put("PICK", PICK);
//		maps.put("CC", CC);
//		maps.put("PY", CC);

		return statusList;
	}

	@Override
	public List<Map<String, Object>> getLocationUse() {
		List<Map<String, Object>> locationUse = taskDao.getLocationUse();
		return locationUse;
	}

	@Override
	public Map<String, BigDecimal> getSkuCount() {
		List<Map<String, Object>> skuCountMap = taskDao.getSkuCount();

		QueryWrapper<SkucategoryEntity> query = new QueryWrapper<SkucategoryEntity>();
		query.orderByAsc("PID");
		List<SkucategoryEntity> skuCategorys = skucategoryDao.selectList(query);

		Map<String, BigDecimal> returnMap = new HashMap<String, BigDecimal>();

		for (Map map : skuCountMap) {
			String categorykey = (String) map.get("categorykey");

			BigDecimal qty = (BigDecimal) map.get("qty");

			SkucategoryEntity skucategoryEntity = null;

			if (StringUtils.isNotBlank(categorykey)) {
				skucategoryEntity = getSkuCategory(categorykey, skuCategorys);
			}

			String key = "";
			if (skucategoryEntity == null) {
				key = "";
			} else {
				key = skucategoryEntity.getName();

			}

			if (returnMap.containsKey(key)) {
				BigDecimal tmpQty = returnMap.get(key);

				returnMap.put(key, tmpQty.add(qty));
			} else {
				returnMap.put(key, qty);
			}
		}

		return returnMap;
	}

	private SkucategoryEntity getSkuCategory(String categorykey, List<SkucategoryEntity> skuCategorys) {
		for (SkucategoryEntity skucategoryEntity : skuCategorys) {
			String tmpCategorykey = skucategoryEntity.getCategorykey();
			if (categorykey.equals(tmpCategorykey)) {
				Long pid = skucategoryEntity.getPid();
				Long id = skucategoryEntity.getId();
				if (pid == 0) {
					return skucategoryEntity;
				}
				return getSkuCategory(pid, skuCategorys);
			}
		}
		return null;
	}

	private SkucategoryEntity getSkuCategory(Long pid, List<SkucategoryEntity> skuCategorys) {
		for (SkucategoryEntity skucategoryEntity : skuCategorys) {
			Long tmppid = skucategoryEntity.getPid();
			Long tmpid = skucategoryEntity.getId();
			if (tmpid.equals(pid)) {
				if (tmppid == 0) {
					return skucategoryEntity;
				}
				return getSkuCategory(tmppid, skuCategorys);
			}
		}
		return null;
	}

	@Override
	public List<Map<String, Object>> getDDJStatus() {
		List<Map<String, Object>> errorMsg = taskDao.getErrorMsg();

		List<Map<String, Object>> ddjList = taskDao.getDdjList();

		List<Map<String, Object>> returnList = new ArrayList<Map<String, Object>>();

		for (Map map : ddjList) {
			String COMMENTS = (String) map.get("COMMENTS");
			String err1 = (String) map.get("err1");
			String err2 = (String) map.get("err2");
			String ip = (String) map.get("DVC_IP");
			String port = (String) map.get("PORT");

			Object msg = "正常";
			Map<String, Object> returnmap = new HashMap<String, Object>();

			boolean isRunning = isRunning(ip, port);
			if (!isRunning) {
				msg = "未连接";
			}

//			char[] charErr1 = err1.toCharArray();
//			int i = 0;
//			for (; i < charErr1.length; i++) {
//				char tem = charErr1[i];
//				if (tem == '1') {
//					for (Map ddjmap : errorMsg) {
//						String id = (String) ddjmap.get("id");
//						String code = (String) ddjmap.get("code");
//						if (id.equals((i + 1) + "")) {
//							msg = code;
//						}
//					}
//				}
//
//			}
//			char[] charErr2 = err2.toCharArray();
//			for (; i - charErr1.length < charErr2.length; i++) {
//				char tem = charErr1[i - charErr1.length];
//				if (tem == '1') {
//					for (Map ddjmap : errorMsg) {
//						String id = (String) ddjmap.get("id");
//						String code = (String) ddjmap.get("code");
//						if (id.equals((i + 1) + "")) {
//							msg = code;
//						}
//					}
//				}
//			}
			if (msg == "正常") {
				if (err1.indexOf('1') > 0 || err2.indexOf('1') > 0) {
					msg = "异常";
				}
			}

			returnmap.put("COMMENTS", COMMENTS);
			returnmap.put("msg", msg);
			returnList.add(returnmap);
		}

		return returnList;
	}

	private boolean isRunning(String ip, String port) {
		Socket sClient = null;
		try {
			SocketAddress saAdd = new InetSocketAddress(ip.trim(), Integer.parseInt(port));
			sClient = new Socket();
			sClient.connect(saAdd, 500); // 设置超时 3s
		} catch (Exception e) {
			return false;
		} finally {
			try {
				if (sClient != null) {
					sClient.close();
				}
			} catch (Exception e) {
			}
		}
		return true;
	}

	@Override
	public Map<String, Object> getLast7Days() {
		List<Map<String, Object>> ddjList = taskDao.getLast7Days();

		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		GregorianCalendar cal = new GregorianCalendar();
		String sday = dateFormat.format(cal.getTime());
		cal.add(6, -1);
		String sday1 = dateFormat.format(cal.getTime());
		cal.add(6, -1);
		String sday2 = dateFormat.format(cal.getTime());
		cal.add(6, -1);
		String sday3 = dateFormat.format(cal.getTime());
		cal.add(6, -1);
		String sday4 = dateFormat.format(cal.getTime());
		cal.add(6, -1);
		String sday5 = dateFormat.format(cal.getTime());
		cal.add(6, -1);
		String sday6 = dateFormat.format(cal.getTime());

		List<String> dats = new ArrayList<String>();
		dats.add(sday6);
		dats.add(sday5);
		dats.add(sday4);
		dats.add(sday3);
		dats.add(sday2);
		dats.add(sday1);
		dats.add(sday);

		List<BigDecimal> datePa = new ArrayList<BigDecimal>();
		List<BigDecimal> datePick = new ArrayList<BigDecimal>();
		for (int i = 0; i < dats.size(); i++) {
			String dayStr = dats.get(i);
			datePa.add(BigDecimal.ZERO);
			datePick.add(BigDecimal.ZERO);
			for (Map map : ddjList) {
				String dataMap = (String) map.get("date");
				BigDecimal qtyMap = (BigDecimal) map.get("qty");
				String typeMap = (String) map.get("tradetype");
				if (dayStr.equals(dataMap)) {
					if (typeMap.equals("PA") || typeMap.equals("RPA")) {
						BigDecimal paQty = datePa.get(i);
						datePa.set(i, paQty.add(qtyMap));
					} else if (typeMap.equals("PICK")) {
						BigDecimal pickQty = datePick.get(i);
						datePick.set(i, pickQty.add(qtyMap));
					}
				}

			}
		}

		Map<String, Object> renturnMap = new HashMap<String, Object>();
		renturnMap.put("dayStr", dats);
		renturnMap.put("paQty", datePa);
		renturnMap.put("pickQty", datePick);

		return renturnMap;
	}

	@Override
	public Map<String, Object> getLast30Days() {
		List<Map<String, Object>> last30DaysList = taskDao.getLast30Days();

		//近30天日期
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
		GregorianCalendar cal = new GregorianCalendar();
		cal.add(6, -29);
		List<String> dats = new ArrayList<String>();
		dats.add(dateFormat.format(cal.getTime()));
		for (int i = 0; i < 29; i++) {
			cal.add(Calendar.DAY_OF_YEAR, 1);
			dats.add(dateFormat.format(cal.getTime()));
		}


		List<BigDecimal> datePa = new ArrayList<BigDecimal>();   //入库数量
		List<BigDecimal> datePick = new ArrayList<BigDecimal>(); //出库数量
		List<BigDecimal> datePY = new ArrayList<BigDecimal>();   //领用数量
		for (int i = 0; i < dats.size(); i++) {
			String dayStr = dats.get(i);
			datePa.add(BigDecimal.ZERO);
			datePick.add(BigDecimal.ZERO);
			datePY.add(BigDecimal.ZERO);
			for (Map map : last30DaysList) {
				String dataMap = (String) map.get("addtime");
				BigDecimal qtyMap = (BigDecimal) map.get("qty");
				String typeMap = (String) map.get("tradetype");
				if (dayStr.equals(dataMap)) {
					if (typeMap.equals("PA") ) {
						BigDecimal paQty = datePa.get(i);
						datePa.set(i, paQty.add(qtyMap));
					} else if (typeMap.equals("PICK")) {
						BigDecimal pickQty = datePick.get(i);
						datePick.set(i, pickQty.add(qtyMap));
					}else if (typeMap.equals("PY")) {
						BigDecimal pyQty = datePY.get(i);
						datePY.set(i, pyQty.add(qtyMap));
					}
				}
			}
		}

		Map<String, Object> renturnMap = new HashMap<String, Object>();
		renturnMap.put("dayStr", dats);
		renturnMap.put("paQty", datePa);
		renturnMap.put("pickQty", datePick);
		renturnMap.put("pyQty", datePY);

		return renturnMap;
		
	}

	@Override
	public Map<String, Object> getLastYear() {
		List<Map<String, Object>> last30DaysList = taskDao.getLastYear();

		//近1年
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
		GregorianCalendar cal = new GregorianCalendar();
		cal.add(Calendar.YEAR, -1);
		List<String> dats = new ArrayList<String>();
		for (int i = 0; i < 12; i++) {
			cal.add(Calendar.MONTH, 1);
			dats.add(dateFormat.format(cal.getTime()));
		}


		List<BigDecimal> datePa = new ArrayList<BigDecimal>();   //入库数量
		List<BigDecimal> datePick = new ArrayList<BigDecimal>(); //出库数量
		List<BigDecimal> datePY = new ArrayList<BigDecimal>();   //领用数量
		for (int i = 0; i < dats.size(); i++) {
			String dayStr = dats.get(i);
			datePa.add(BigDecimal.ZERO);
			datePick.add(BigDecimal.ZERO);
			datePY.add(BigDecimal.ZERO);
			for (Map map : last30DaysList) {
				String dataMap = (String) map.get("addtime");
				BigDecimal qtyMap = (BigDecimal) map.get("qty");
				String typeMap = (String) map.get("tradetype");
				if (dayStr.equals(dataMap)) {
					if (typeMap.equals("PA") ) {
						BigDecimal paQty = datePa.get(i);
						datePa.set(i, paQty.add(qtyMap));
					} else if (typeMap.equals("PICK")) {
						BigDecimal pickQty = datePick.get(i);
						datePick.set(i, pickQty.add(qtyMap));
					}else if (typeMap.equals("PY")) {
						BigDecimal pyQty = datePY.get(i);
						datePY.set(i, pyQty.add(qtyMap));
					}
				}
			}
		}

		Map<String, Object> renturnMap = new HashMap<String, Object>();
		renturnMap.put("dayStr", dats);
		renturnMap.put("paQty", datePa);
		renturnMap.put("pickQty", datePick);
		renturnMap.put("pyQty", datePY);

		return renturnMap;
	}

	@Override
	public List<Map<String, Object>> getInventorAlarm() {
		
		List<Map<String, Object>> inventorAlarm = taskDao.getInventorAlarm();
		return inventorAlarm;
		
		
	}

	@Override
	public List<Map<String, Object>> getTodayTaskInfo() {
		List<Map<String, Object>> inventorAlarm = taskDao.getTodayTaskInfo();
		return inventorAlarm;
		
	}



	@Override
	public IPage<TaskEntity> getTodayTaskInfo2(Map<String, Object> params) {
		QueryWrapper<TaskEntity> wrapper = new QueryWrapper<>();
		wrapper.orderByDesc("a.addtime");
		IPage<TaskEntity> inventorAlarm = taskDao.getTodayTaskInfo2(getPage(params, null, false),wrapper);
		return inventorAlarm;
	}

	@Override
	public Integer getSkuSum() {
		Integer skuSum = taskDao.getSkuSum();
		return skuSum;
	}

	@Override
	public BigDecimal getWeigth() {
		BigDecimal weigth = taskDao.getWeigth();
		return weigth;
	}

	@Override
	public Integer getlocateCount(){

		Integer locatecount = taskDao.getlocateCount();
		return locatecount;
	}

	@Override
	public Integer getlocateSY(){

		Integer locatesy = taskDao.getlocateSY();
		return locatesy;
	}


}
